package _08_DFS_BFS;

import java.util.Arrays;

public class _733_图像渲染 {

    public static void main(String[] args) {
        _733_图像渲染 v = new _733_图像渲染();

        int[][] ins = {
                {1, 1, 1},
                {1, 1, 0},
                {1, 0, 1}
        };

        v.floodFill(ins, 1, 1, 2);
        for (int[] in : ins) {
            System.out.println(Arrays.toString(in));
        }

    }

    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int oldColor = image[sr][sc];
        dfs(image, sr, sc, oldColor, newColor);
        return image;
    }

    public void dfs(int[][] image, int sr, int sc, int oldColor, int newColor) {
        if (sr < 0 || sr >= image.length || sc < 0 || sc >= image[sr].length || image[sr][sc] == newColor || image[sr][sc] != oldColor) {
            return;
        }
        image[sr][sc] = newColor;
        dfs(image, sr - 1, sc, oldColor, newColor);
        dfs(image, sr + 1, sc, oldColor, newColor);
        dfs(image, sr, sc - 1, oldColor, newColor);
        dfs(image, sr, sc + 1, oldColor, newColor);
    }

}
